package com.mx.nbainformation.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.mx.nbainformation.R;
import com.mx.nbainformation.Model.NewsContentData;
import com.mx.nbainformation.Model.NewsListData;
import com.mx.nbainformation.base.BaseActivity;
import com.mx.nbainformation.biz.BizNews;
import com.umeng.analytics.MobclickAgent;

public class ActivityNews extends BaseActivity implements OnClickListener,
		OnItemClickListener {
	BizNews _bizNews;
	private Boolean isFirst = true;

	private ListView _lvNews;
	private SimpleAdapter _adapter;
	private List<Map<String, Object>> _list;

	// views
	private Button _btnPrevious;
	private Button _btnNext;
	private Button _btnReload;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news);
		initClass();
		initNoItems();
		initControl();
		initListView();
		new DataAsyncTask().execute(false, false);
	}

	private void initClass() {
		_bizNews = new BizNews(this);
	}

	private void initControl() {
		_lvNews = (ListView) findViewById(R.id.listNews);
		_btnPrevious = (Button) findViewById(R.id.btnPrevious);
		_btnNext = (Button) findViewById(R.id.btnNext);
		_btnPrevious.setOnClickListener(this);
		_btnNext.setOnClickListener(this);
		_btnReload = (Button) findViewById(R.id.btnReload);
		_btnReload.setOnClickListener(this);
	}

	private void initListView() {
		_list = new ArrayList<Map<String, Object>>();
		_adapter = new SimpleAdapter(ActivityNews.this, _list,
				R.layout.item_news_list, new String[] { NewsListData.CONTENT,
						NewsListData.NEWSDATE }, new int[] {
						R.id.item_news_content, R.id.item_news_date });
		_lvNews.setAdapter(_adapter);
		_lvNews.setOnItemClickListener(this);
	}

	class DataAsyncTask extends
			AsyncTask<Boolean, Void, List<Map<String, Object>>> {

		@Override
		protected List<Map<String, Object>> doInBackground(Boolean... params) {
			// TODO Auto-generated method stub
			String url = _bizNews.getUrl(params[0], params[1]);
			return _bizNews.getNewsList(url);
		}

		@Override
		protected void onPostExecute(List<Map<String, Object>> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (mAlertDialog != null)
				mAlertDialog.dismiss();
			if (result != null && result.size() > 0) {
				_noItem.setVisibility(8);
				_list.addAll(result);
				_adapter.notifyDataSetChanged();
			} else {
				_noItem.setVisibility(0);
			}
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			if (!isFirst) {
				showProgressDialog(
						getResources().getString(R.string.LoadingTitle),
						getResources().getString(R.string.LoadingContext), null);
			} else {
				isFirst = false;
			}
			if (_list.size() > 0) {
				_list.clear();
				_adapter.notifyDataSetChanged();
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String url;
		switch (v.getId()) {
		case R.id.btnNext:
			url = _bizNews.getUrl(false, true);
			if (url == null || url.equals("#") || url.equals("")) {
				showToast(getResources().getString(R.string.noNext));
				return;
			}
			new DataAsyncTask().execute(false, true);
			MobclickAgent.onEvent(this, "news", "btnNext");
			break;
		case R.id.btnPrevious:
			url = _bizNews.getUrl(true, false);
			if (url == null || url.equals("#") || url.equals("")) {
				showToast(getResources().getString(R.string.noPrevious));
				return;
			}
			new DataAsyncTask().execute(true, false);
			MobclickAgent.onEvent(this, "news", "btnPrevious");
			break;
		case R.id.btnReload:
			new DataAsyncTask().execute(false, false);
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		HashMap<String, Object> hash = (HashMap<String, Object>) _lvNews
				.getItemAtPosition(arg2);
		try {
			MobclickAgent.onEvent(this, "news", "itemClick");
		} catch (Exception ex) {
		}
		Intent intent = new Intent();
		intent.setClass(ActivityNews.this, ActivityWebContent.class);
		intent.putExtra(NewsContentData.TARGET, hash.get(NewsListData.TARGER)
				.toString());
		startActivity(intent);
	}
}
